<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/6
 * Time: 16:35
 */

namespace YMApi\Controller;


use Think\Controller;

/**
 * Class ShelvesGoodsServiceController
 * @package SDXApi\Controller
 */
class ShelvesGoodsServiceController extends Controller
{
	/**
	 * @var \Model|\Think\Model
	 */
	protected $shelvesModel;
	/**
	 * @var \Model|\Think\Model
	 */
	protected $operateLogModel;

	/**
	 * ShelvesGoodsServiceController constructor.
	 */
	public function __construct()
	{
		$this->shelvesModel = D('Shelves');
		$this->operateLogModel = D('OperateLog');
	}

	/**
	 * 预警入口
	 * Created by Jerry
	 */
	public function handle()
	{
		$this->delete_operate_by_shelves_type();
		$this->shelvesService();
	}

	/**
	 * 处理预警服务
	 * Created by Jerry
	 */
	protected function shelvesService()
	{
	    ini_set('max_execution_time', '0');
	    set_time_limit(0);
		$data = $this->getShelvesGoods();
		$this->checkShelvesService($data);
		//有库存的商品列表数据
		$gsdata=$this->shelvesModel->get_goods_storage_data();
		//商品列表数据
		$gdata=$this->shelvesModel->get_goods_data();
		if($gsdata || $gdata){
		    foreach ($data as $key=>$val){
		        foreach ($gsdata as $k=>$v){
		            if($val['goods_id']==$v['goods_id']){
		                unset($gsdata[$k]);
		            }
		        }
		        
		        foreach ($gdata as $k1=>$v1){
		            if($val['goods_id']==$v1['goods_id']){
		                unset($gdata[$k1]);
		            }
		        }
		    }
		}
		$operate_log_mod=M('operate_log');
		if(count($gsdata)>0){
		    $insertData = array();
		    foreach ($gsdata as $k=>$v){  //撤架有库存
		        $data = array(
		            'goods_sn'	=>	$v['goods_sn'],
		            'goods_id'	=>	$v['goods_id'],
		            'goods_name'=>	$v['goods_name'],
		            'type'		=>	11,
		        );
		        $insertData[] =$data;
		        //$operate_log_mod->add($data);
		    }
		    //对预警数据进行分组
		    $chunkData = array_chunk($insertData, 200);
		    
		    $this->operateLogModel->insert_sales_goods_for_operate_all($chunkData);
		}
		
		if(count($gdata)>0){
		    $insertData = array();
		    foreach ($gdata as $k1=>$v1){ 
		       $type='';
		       if($v1['is_on_sale']==1){//撤架已上架
		           $type=8;
		       }elseif($v1['is_on_sale']==0){  //撤架未上架
		           $type=9;
		       }else{
		           continue;
		       }
		       $data = array(
		           'goods_sn'	=>	$v1['goods_sn'],
		           'goods_id'	=>	$v1['goods_id'],
		           'goods_name'=>	$v1['goods_name'],
		           'type'		=>	$type,
		       );
		       $insertData[] =$data;
		       //$operate_log_mod->add($data);
		   }
		   //对预警数据进行分组
		   $chunkData = array_chunk($insertData, 200);
		   
		   $this->operateLogModel->insert_sales_goods_for_operate_all($chunkData);
		}
	}

	
	
	/**
	 * 获取货价商品
	 * Created by Jerry
	 * @return mixed
	 */
	protected function getShelvesGoods()
	{
		return $this->shelvesModel->get_shelves_service_goods_for_list();
	}

	/**
	 * 循环处理并插入预警
	 * Created by Jerry
	 * @param $data
	 * @return bool
	 */
	protected function checkShelvesService($data)
	{
		if (empty($data)) {
			return false;
		}

		$insertData = array();
		foreach ($data as $k => $v) {
			$goods = $this->checkShelvesData($v);

			if (!$goods) {
				continue;
			}

			$insertData[] = $goods;

		}

//		对预警数据进行分组
		$chunkData = array_chunk($insertData, 200);
		
		$this->operateLogModel->insert_sales_goods_for_operate_all($chunkData);
	}

	/**
	 * 处理预警类型
	 * Created by Jerry
	 * @param $goods
	 * @return bool
	 */
	protected function checkShelvesData($goods)
	{
//		在架未上架
		if ($goods['state'] == 1 && $goods['is_on_sale'] == 0) {
			$goods['type'] = 7;
		}
		

//		撤架/已上架
		if ($goods['state'] == 0 && $goods['is_on_sale'] == 1) {
			$goods['type'] = 8;
		}

//		撤架/未上架
		if ($goods['state'] == 0 && $goods['is_on_sale'] == 0) {
			$rs = $this->checkShelvesStorage($goods['goods_id']);
//			判断是否有库存
			if ($rs > 0) {
				$goods['type'] = 9;
			}
		}
		
		//撤架/有库存
		if($goods['state']==0 && $goods['practical_num']>0){
		    $goods['type'] =11;
		}
		
		//在架/无库存
		if($goods['state']==1 && $goods['practical_num']<=0){
		    $goods['type'] =12;
		}
		

		if (empty($goods['type'])) {
			return false;
		}

		return $goods;
	}

	/**
	 * 检查实际库存
	 * Created by Jerry
	 * @param $goods_id
	 * @return mixed
	 */
	protected function checkShelvesStorage($goods_id)
	{
		return $this->shelvesModel->get_shelves_service_goods_storage($goods_id);
	}

	/**
	 * 删除所有促销类型数据
	 * Created by Jerry
	 */
	protected function delete_operate_by_shelves_type()
	{
		$where = array(
			'type'	=>	array('IN', '7, 8, 9,11,12'),
		);

		$this->operateLogModel->detele_operate_by_sales_type($where);
	}
	
	
}